/**
 * Copyright 2016 JustWayward Team
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kanshu.ksgb.fastread.doudou.module.reader.utils;

import android.graphics.Bitmap;
import android.util.LruCache;
import android.view.View;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.ReadTheme;
import com.kanshu.ksgb.fastread.doudou.module.reader.page.PageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yuyh.
 * @date 2016/9/23.
 */
public class ThemeManager {

    public static final int NORMAL = 0;
    public static final int YELLOW = 1;
    public static final int GREEN = 2;
    public static final int LEATHER = 3;
    public static final int GRAY = 4;
    public static final int NIGHT = 5;
    private static int sAdHeight = 0;
    public static LruCache<Integer, Bitmap> sLruCache = new LruCache<>(5);

    public static void setReaderTheme(int theme, View view) {
        switch (theme) {
            case NORMAL:
                view.setBackgroundResource(R.drawable.theme_white_bg);
                break;
            case YELLOW:
                view.setBackgroundResource(R.drawable.theme_yellow_bg);
                break;
            case GREEN:
                view.setBackgroundResource(R.drawable.theme_green_bg);
                break;
            case LEATHER:
                view.setBackgroundResource(R.drawable.theme_leather_color_bg);
                break;
            case GRAY:
                view.setBackgroundResource(R.drawable.theme_gray_bg);
                break;
            case NIGHT:
                view.setBackgroundResource(R.drawable.theme_night_bg);
                break;
            default:
                break;
        }
    }

    /**
     * 获取bitmap
     * @param mark
     * @return
     */
    public static Bitmap getCurNextBitmap(int mark) {
        Bitmap soft = sLruCache.get(mark);
        if (soft == null || soft.isRecycled()) {
            soft = Bitmap.createBitmap(DisplayUtils.getScreenWidth(Xutils.getContext()), DisplayUtils.getScreenHeight(Xutils.getContext()), Bitmap.Config.ARGB_8888);
            sLruCache.put(mark, soft);
        }
        return soft;
    }

    public static List<ReadTheme> getReaderThemeData(int curTheme) {
        int[] themes = {NORMAL, YELLOW, GREEN, LEATHER, GRAY, NIGHT};
        List<ReadTheme> list = new ArrayList<>();
        ReadTheme theme;
        for (int i = 0; i < themes.length; i++) {
            theme = new ReadTheme();
            theme.theme = themes[i];
            list.add(theme);
        }
        return list;
    }

    public static void clear() {
        if (sLruCache != null) {
            sLruCache.evictAll();
        }
    }

    public static Bitmap getBitmap(int key) {
        Bitmap bitmap = sLruCache.get(key);
        if (bitmap == null || bitmap.isRecycled()) {
            bitmap = Bitmap.createBitmap(DisplayUtils.getScreenWidth(Xutils.getContext()), DisplayUtils.getScreenHeight(Xutils.getContext()) + PageLoader.mNotchHeight + sAdHeight, Bitmap.Config.RGB_565);
            sLruCache.put(key, bitmap);
        }
        return bitmap;
    }

    /**
     * 设置阅读器广告高度
     * @param adHeight
     */
    public static void setAdHeight(int adHeight){
        sAdHeight = adHeight;
    }

}
